package com.atguigu.flink.watermark;

import com.atguigu.flink.function.MyUtil;
import com.atguigu.flink.function.WaterSensorMapFunction;
import com.atguigu.flink.pojo.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 *
 *
 *      如果下游从上有的多个并行度获取数据，假设上游的某个subTask迟迟没有数据产生，导致watermark无法更新和推进，
 *          设置一个时间间隔，超过这个时间间隔，上游的某个subTask依旧没有数据产生，就退出水印的传递。
 */
public class Demo5_WaterMarkIdless
{
    public static void main(String[] args) {

       Configuration conf = new Configuration();
       conf.setInteger("rest.port", 3333);
       StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);


        env.getConfig().setAutoWatermarkInterval(2000);

        env.setParallelism(2);

        WatermarkStrategy<WaterSensor> watermarkStrategy = WatermarkStrategy.<WaterSensor>forMonotonousTimestamps()
            .withTimestampAssigner( (e, r) -> e.getTs())
            //上游的某个subTask 10秒内没有新的数据产生，退出水印的传递。赋闲。直到有新的数据产生，还可以继续传递水印
            .withIdleness(Duration.ofSeconds(10));

        env
            .socketTextStream("hadoop103", 8888)
            .global()
            .map(new WaterSensorMapFunction()).setParallelism(6)
            .assignTimestampsAndWatermarks(watermarkStrategy)
            .keyBy(WaterSensor::getId)
            .window(TumblingEventTimeWindows.of(Time.seconds(5)))
            .process(new ProcessWindowFunction<WaterSensor, String, String, TimeWindow>()
            {
                @Override
                public void process(String s, Context context, Iterable<WaterSensor> elements, Collector<String> out) throws Exception {
                    out.collect(MyUtil.parseIterable(elements).toString());
                }
            })
            .print();


                try {
                            env.execute();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

    }
}
